package com.gx.server.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gx.server.mapper.ExamErrorsMapper;
import com.gx.server.model.domain.ExamErrors;
import com.gx.server.model.domain.ExamQuestion;
import com.gx.server.model.domain.ExamRight;
import com.gx.server.model.service.ExamErrorsService;
import com.gx.server.model.service.ExamQuestionService;
import com.gx.server.model.service.ExamRightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */

@Service
public class ExamErrorsServiceImpl extends ServiceImpl<ExamErrorsMapper, ExamErrors> implements ExamErrorsService {

    @Autowired
    ExamRightService examRightService;

    @Override
    public void weight_drop(Long id, List<ExamQuestion> examQuestions) {
        System.out.println("size---");
        System.out.println(examQuestions.size());


        for (ExamQuestion examQuestion : examQuestions) {

            //正确题目处理
            QueryWrapper<ExamRight> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("user_id",id);
            queryWrapper.eq("question",examQuestion.getId());
            ExamRight one = examRightService.getOne(queryWrapper);
            if (one==null){
                ExamRight examRight = new ExamRight();
                examRight.setUserId(id);
                examRight.setQuestion(examQuestion.getId());
                examRightService.save(examRight);
            }

//            在交卷 该题答对后 对错题库的该题目进行权重的下降
            ExamErrors examErrors = this.ErrorsByUserId(id, examQuestion.getId());

            //如果找到该题目也再错题库中
            if (examErrors!=null){
                System.out.println("--------");
                double weight = examErrors.getWeight();
                double v = weight - 0.5;
                if (v<=0) v =0;
                examErrors.setWeight(v);
                this.updateById(examErrors);
            }

        }

    }

    @Override
    public void Weight_rise(Long id, List<ExamQuestion> examQuestions) {
        for (ExamQuestion examQuestion : examQuestions) {
            //            在交卷 该题答错后 对错题库的该题目进行权重的上降（又答错了
            ExamErrors examErrors = this.ErrorsByUserId(id, examQuestion.getId());

            //如果找到该题目也再错题库中
            if (examErrors!=null){
                double weight = examErrors.getWeight();
                double v = weight + 0.5;
                if (v>=1) v =1;
                examErrors.setWeight(v);
                this.updateById(examErrors);
            }else {
                ExamErrors examErrors_new = new ExamErrors();
                examErrors_new.setUserId(id);
                examErrors_new.setQuestionId(examQuestion.getId());
                examErrors_new.setWeight(1);
                this.save(examErrors_new);
            }

        }

    }


    @Override
    public String del_Errors(Long id, int questuinId) {
        LambdaQueryWrapper<ExamErrors> examCollectLambdaQueryWrapper = new LambdaQueryWrapper<ExamErrors>();
        examCollectLambdaQueryWrapper.eq(ExamErrors::getUserId,id);
        examCollectLambdaQueryWrapper.eq(ExamErrors::getQuestionId,questuinId);
        boolean remove = this.remove(examCollectLambdaQueryWrapper);

        if (remove){
            return "删除成功";
        }else {
            return "删除失败";
        }

    }



    @Override
    public ExamErrors ErrorsByUserId(Long id, int questuinId) {
        LambdaQueryWrapper<ExamErrors> examCollectLambdaQueryWrapper = new LambdaQueryWrapper<ExamErrors>();
        examCollectLambdaQueryWrapper.eq(ExamErrors::getUserId,id);
        examCollectLambdaQueryWrapper.eq(ExamErrors::getQuestionId,questuinId);
        ExamErrors one = this.getOne(examCollectLambdaQueryWrapper);
        return one;
    }
}
